using ASP;
using HoWave.Common;
using HoWave.Web.Admin;
using HoWave.Web.DataHelper;
using System;
using System.Data;
using System.Web.Profile;
using System.Web.SessionState;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
public class Admin_Admin_Spec : Admin, IRequiresSessionState
{
	protected Repeater Repeater1;
	protected LinkButton linkDelete;
	protected Literal ltrPage;
	protected HtmlInputText s_name;
	protected HtmlInputText s_sort;
	protected RadioButton s_dtype_text;
	protected RadioButton s_dtype_image;
	protected Repeater rptSpecValue;
	private int _returnDataCountForEach = 12;
	private int _AllCount = -1;
	private Shop_SepcBll _sepc;
	public string _isView = "";
	protected DefaultProfile Profile
	{
		get
		{
			return (DefaultProfile)this.Context.Profile;
		}
	}
	protected global_asax ApplicationInstance
	{
		get
		{
			return (global_asax)this.Context.ApplicationInstance;
		}
	}
	protected int ReturnDataCountForEach
	{
		get
		{
			return this._returnDataCountForEach;
		}
		set
		{
			this._returnDataCountForEach = value;
		}
	}
	protected int AllCount
	{
		get
		{
			int allCount;
			if (this._AllCount != -1)
			{
				allCount = this._AllCount;
			}
			else
			{
				DataTable dataTable = this.conn.GetDataTable("select count(*) as AllCount  from HLShop_spec  ");
				if (dataTable != null && dataTable.Rows.Count > 0)
				{
					this._AllCount = Convert.ToInt32(dataTable.Rows[0]["AllCount"]);
				}
				allCount = this._AllCount;
			}
			return allCount;
		}
		set
		{
			this._AllCount = value;
		}
	}
	public string OAction
	{
		get
		{
			return Fetch.Get("op");
		}
	}
	public string SpID
	{
		get
		{
			return Fetch.Get("sp_id");
		}
	}
	public Shop_SepcBll Sepc
	{
		get
		{
			Shop_SepcBll sepc;
			if (this._sepc == null)
			{
				this._sepc = new Shop_SepcBll();
				sepc = this._sepc;
			}
			else
			{
				sepc = this._sepc;
			}
			return sepc;
		}
		set
		{
			this._sepc = value;
		}
	}
	public string lblParam
	{
		get;
		set;
	}
	protected void Page_Load(object sender, EventArgs e)
	{
		if (!base.IsPostBack)
		{
			this.Bind();
			this.GetPageLink();
			this.GetSpecValue();
		}
	}
	private void GetSpecValue()
	{
		string text = Fetch.Get("sp_id");
		int num = 0;
		if (text != string.Empty && int.TryParse(text, out num))
		{
			DataTable dataTable = this.conn.GetDataTable("select * from dbo.HLshop_spec_value where sp_id=" + text);
			this.rptSpecValue.DataSource = dataTable;
			this.rptSpecValue.DataBind();
			dataTable.Dispose();
		}
	}
	public void Bind()
	{
		if (Fetch.Get("op") == "spec_add")
		{
			this.lblParam = "add";
		}
		else
		{
			if (Fetch.Get("op") == "spec_edit")
			{
				try
				{
					int sp_id = Convert.ToInt32(Fetch.Get("sp_id").ToString());
					DataTable specBysp_id = this.Sepc.GetSpecBysp_id(sp_id);
					this.s_name.Value = specBysp_id.Rows[0]["sp_name"].ToString();
					this.s_sort.Value = specBysp_id.Rows[0]["sp_sort"].ToString();
					string a = specBysp_id.Rows[0]["sp_format"].ToString();
					if (a == "text")
					{
						this.s_dtype_text.Checked = true;
					}
					else
					{
						this.s_dtype_image.Checked = true;
					}
					this.lblParam = "edit";
				}
				catch (Exception)
				{
					throw;
				}
			}
			else
			{
				this.lblParam = "spec";
			}
		}
		int pageIndex = this.ValidateQueString();
		this.Repeater1.DataSource = this.Sepc.GetSepcAll(pageIndex, this.ReturnDataCountForEach);
		this.Repeater1.DataBind();
		foreach (RepeaterItem repeaterItem in this.Repeater1.Items)
		{
			HtmlInputCheckBox htmlInputCheckBox = (HtmlInputCheckBox)repeaterItem.FindControl("itemsbox");
			LinkButton linkButton = (LinkButton)repeaterItem.FindControl("linkDel");
			Literal literal = (Literal)repeaterItem.FindControl("lp");
			if (htmlInputCheckBox.Value == "1")
			{
				literal.Visible = false;
				linkButton.Visible = false;
				htmlInputCheckBox.Disabled = true;
			}
		}
	}
	public string GetValueNameBySpID(object obj)
	{
		return this.conn.GetScalar("declare @aa varchar(100)\r\n                          set @aa=''\r\n                         select @aa+=sp_value_name+',' from HLshop_spec_value where sp_id=" + obj.ToString() + " select @aa ", new IDbDataParameter[0]).ToString();
	}
	public string IsView()
	{
		string isView;
		if (this._isView != "")
		{
			isView = this._isView;
		}
		else
		{
			IDbDataParameter dbDataParameter = this.conn.CreateParameter("@sp_id", DbType.Int32, this.SpID);
			if (this.conn.ExecuteCount("select COUNT(*) from dbo.HLshop_spec_value \r\n                            where sp_id =@sp_id and sp_value_image is not null and sp_value_image !='' ", new IDbDataParameter[]
			{
				dbDataParameter
			}) > 0)
			{
				this._isView = "block";
			}
			else
			{
				this._isView = "none";
			}
			isView = this._isView;
		}
		return isView;
	}
	private string GetPage(int yushu, int page, int last, int allpage)
	{
		string text = base.Request.Url.ToString().ToLower();
		int begin = text.LastIndexOf("page=");
		int deletePoint = this.GetDeletePoint(begin, text);
		string text2 = "";
		for (int i = 1; i <= yushu; i++)
		{
			if (page == last && page != allpage && page % 10 != 0)
			{
				object obj = text2;
				text2 = string.Concat(new object[]
				{
					obj,
					"<li><a  href='?page=",
					last + i,
					"'><span class='",
					(page == last + i - 1) ? "currentpage" : "",
					"'>",
					last + i,
					"</span></a></li>"
				});
			}
			else
			{
				if (page == last && page == allpage)
				{
					last -= 10;
					object obj = text2;
					text2 = string.Concat(new object[]
					{
						obj,
						"<li><a  href='?page=",
						last + i,
						"'><span class='",
						(page == last + i) ? "currentpage" : "",
						"'>",
						last + i,
						"</span></a></li>"
					});
				}
				else
				{
					if (page == last && page != allpage && page % 10 == 0)
					{
						last -= 10;
						object obj = text2;
						text2 = string.Concat(new object[]
						{
							obj,
							"<li><a  href='?page=",
							last + i,
							"'><span class='",
							(page == last + i - 1) ? "currentpage" : "",
							"'>",
							last + i,
							"</span></a></li>"
						});
					}
					else
					{
						object obj = text2;
						text2 = string.Concat(new object[]
						{
							obj,
							"<li><a  href='?page=",
							last + i,
							"'><span class='",
							(page == last + i) ? "currentpage" : "",
							"'>",
							last + i,
							"</span></a></li>"
						});
					}
				}
			}
		}
		return text2;
	}
	private void GetPageLink()
	{
		string text = base.Request.Url.ToString().ToLower();
		int begin = text.LastIndexOf("page=");
		int deletePoint = this.GetDeletePoint(begin, text);
		int num = this.ValidateQueString();
		int num2 = (this.AllCount + this.ReturnDataCountForEach - 1) / this.ReturnDataCountForEach;
		if (num2 == 0)
		{
			this.ltrPage.Text = "<li><a  href='javascript:;'><span>上一页</span></a></li><li><a  href='javascript:'><span>下一页</span></a></li>";
		}
		else
		{
			if (num <= num2 && num > 0)
			{
				string text2 = "";
				text2 += ((num == 1) ? "<li><a  href='javascript:;'><span>上一页</span></a></li>" : ("<li><a  href='?page=" + (num - 1) + "'><span>上一页</span></a></li>"));
				if (num > 0 && num <= 10)
				{
					int yushu = (num2 / 10 > 0) ? 10 : num2;
					text2 += this.GetPage(yushu, num, 0, num2);
				}
				else
				{
					if (num > 10 && num <= 10 * (num2 / 10))
					{
						int last = 10 * (num / 10);
						text2 += this.GetPage(10, num, last, num2);
					}
					else
					{
						if (num > 10 * (num2 / 10) && num <= num2)
						{
							int yushu = num2 % 10;
							int last = 10 * (num2 / 10);
							text2 += this.GetPage(yushu, num, last, num2);
						}
					}
				}
				if (num2 == num)
				{
					text2 += "<li><a  href='javascript:'><span>下一页</span></a></li>";
				}
				else
				{
					object obj = text2;
					text2 = string.Concat(new object[]
					{
						obj,
						"<li><a  href='?page=",
						num + 1,
						"'><span>下一页</span></a></li>"
					});
				}
				this.ltrPage.Text = text2;
			}
		}
	}
	private int GetDeletePoint(int begin, string url)
	{
		int result = begin;
		if (begin > 0)
		{
			result = begin + 1;
			for (int i = begin + 1; i < url.Length; i++)
			{
				if (url[i] == '&')
				{
					result = i;
				}
			}
		}
		return result;
	}
	private int ValidateQueString()
	{
		string text = base.Request.QueryString["page"];
		int num = 1;
		int result;
		if (!string.IsNullOrEmpty(text))
		{
			if (!int.TryParse(text, out num))
			{
				result = 1;
			}
			else
			{
				result = num;
			}
		}
		else
		{
			result = 1;
		}
		return result;
	}
	protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
	{
		if (e.CommandName == "del")
		{
			bool deleteSepc = this.Sepc.GetDeleteSepc(Convert.ToInt32(e.CommandArgument));
			if (deleteSepc)
			{
				base.Response.Redirect("Admin_Spec.aspx?op=spec");
			}
		}
	}
	protected void linkDelete_Click(object sender, EventArgs e)
	{
		string text = "";
		foreach (RepeaterItem repeaterItem in this.Repeater1.Items)
		{
			HtmlInputCheckBox htmlInputCheckBox = (HtmlInputCheckBox)repeaterItem.FindControl("itemsbox");
			if (htmlInputCheckBox.Checked)
			{
				text = text + htmlInputCheckBox.Value + ",";
			}
		}
		if (text.Length > 1)
		{
			text = text.Substring(0, text.Length - 1);
		}
		else
		{
			base.Response.Redirect("Admin_Spec.aspx?op=spec");
		}
		bool flag = this.conn.Execute("delete from HLShop_spec where sp_id in (" + text + ")") > 0;
		if (flag)
		{
			base.Response.Redirect("Admin_Spec.aspx?op=spec");
		}
		else
		{
			this.Page.ClientScript.RegisterStartupScript(this.Page.GetType(), "msg", "<script>alert('删除失败！');</script>");
		}
	}
}
